package com.tsugun.mediaplayer.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.tsugun.mediaplayer.R;
import com.tsugun.mediaplayer.presenter.provider.ListProvider;

import java.lang.ref.WeakReference;


public class StartupActivity extends BaseActivity {

    public static final int START_UP = 0x32;
    // 最少等待时间
    public static final long DELAYED_TIME = 1600;

    public boolean isOver;
    public StartupHandler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_startup);
        handler = new StartupHandler(this);
        handler.sendEmptyMessageDelayed(START_UP, DELAYED_TIME);
    }

    @Override
    protected void onMusicLoadFailed() {
        finish();
    }

    @Override
    protected void onMusicLoadComplete(ListProvider musicListProvider) {
        if (!isOver) {
            isOver = true;
            return;
        }
        startup();
    }

    @Override
    public void onSettingChanged() {
        // 应用设置参数
    }

    private void startup() {
        startActivity(new Intent(this, IndexActivity.class));
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private static class StartupHandler extends Handler {

        private WeakReference<StartupActivity> activity;

        StartupHandler(StartupActivity activity) {
            this.activity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            StartupActivity startupActivity = activity.get();
            if (startupActivity != null && msg.what == START_UP) {
                if (startupActivity.isOver) {
                    startupActivity.startup();
                    return;
                }
                startupActivity.isOver = true;
            }
        }
    }

}
